/**
 * 
 */
package com.qolome.service.impl;

import static com.qolome.dao.mapper.UserDynamicSqlSupport.user;
import static com.qolome.dao.mapper.UserDynamicSqlSupport.userKey;
import static com.qolome.dao.mapper.UserDynamicSqlSupport.username;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
import static org.mybatis.dynamic.sql.SqlBuilder.select;

import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qolome.convert.UserConvert;
import com.qolome.dao.mapper.UserMapper;
import com.qolome.dao.model.User;
import com.qolome.dto.request.UserDtoCreateRequest;
import com.qolome.enums.ResultEnum;
import com.qolome.exception.QolomeException;
import com.qolome.service.UserService;
import com.qolome.utils.HashUtil;
import com.qolome.utils.IdUtil;

/**
 * @Description: 用户服务
 * @author 姜锋
 * @date 2020年1月5日 下午3:00:58 
 * @version V1.0   
 *
 */
@Service
public class UserServiceImpl implements UserService{

	
	@Autowired
	UserMapper userMapper;
	
	@Override
	public void create(UserDtoCreateRequest request) {
		User user = UserConvert.userDtoCreateRequest2User(request);
		user.setUserId(IdUtil.getUUid());
		user.setUserKey(HashUtil.getMd5(user.getUserKey()).substring(16));
		userMapper.insertSelective(user);
	}

	/* （非 Javadoc）
	 * @see com.qolome.service.UserService#getKey(java.lang.String)
	 */
	@Override
	public User selectByPrimaryKey(String userId) {
		
		SelectStatementProvider selectStatement  = 
				select(user.userId,userKey,username)
				.from(user)
				.where(user.userId,isEqualTo(userId))
				.build()
				.render(RenderingStrategies.MYBATIS3);
		return userMapper.selectOne(selectStatement).orElseThrow(()->new QolomeException(ResultEnum.USER_NOT_FUND));
	}
	
	
}
